<?php

require_once './include_util.php';
session_start();

$params = getRequestParams();
$action = $params['action'];
$validActions = array('requestToken', 'authorize', 'accessToken', 'logout');
if (!in_array($action, $validActions)) {
	exit();
}

require_once 'HTTP/OAuth.php';
require_once 'HTTP/OAuth/Consumer/Request.php';
require_once 'HTTP/Request2.php';
require_once 'HTTP/OAuth/Consumer.php';
require_once '../lib/Config.php';
require_once '../lib/Store.php';

$httpRequest = new HTTP_Request2;
$httpRequest->setHeader('Accept-Encoding', '.*');
$request = new HTTP_OAuth_Consumer_Request;
$request->accept($httpRequest);

$config = GG_API_Config::getDefault();
$consumer = new HTTP_OAuth_Consumer(
	$config->getOauthConsumerKey(),
	$config->getOauthConsumerSecret()
);

$store = GG_API_Store::getDefault();
$storeKey_oatuhRequestToken = 'oauth_request_token';
$storeKey_oatuhRequestTokenSecret = 'oauth_request_token_secret';
$storeKey_oatuhAccessToken = $store->getKeyNameOfOauthToken();
$storeKey_oatuhAccessTokenSecret = $store->getKeyNameOfOauthTokenSecret();

$gg_url = 'http://www.annonsera.se';
$gg_oauth_url = $gg_url . '/oauth';
$url_suffix = '';

$request_token_url = $gg_oauth_url . '/request_token' . $url_suffix;
$authorize_url = $gg_oauth_url . '/authorize' . $url_suffix;
$access_token_url = $gg_oauth_url . '/access_token' . $url_suffix;

$callback_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']
	. '?action=accessToken';

// Request token --------------------------------------------------------------
if ($action === 'requestToken') {
	$consumer->accept($request);
	try {
		$consumer->getRequestToken($request_token_url, $callback_url);
		$_SESSION[$storeKey_oatuhRequestToken] = $consumer->getToken();
		$_SESSION[$storeKey_oatuhRequestTokenSecret] = $consumer->getTokenSecret();
		$action = 'authorize';
	}
	catch (HTTP_OAuth_Consumer_Exception_InvalidResponse $e) {
		echo get_class($e) . "<br>\n" .
			"<br>\n" . $e->getBody() . "<br>\n";
	}
	catch (Exception $e) {
		echo get_class($e) . ': ' . $e->getMessage() . "\n";
	}
}

// Authorize ------------------------------------------------------------------
if ($action === 'authorize') {
	try {
		$url = $consumer->getAuthorizeUrl($authorize_url);
		ob_end_clean();
		header("Location: $url");
	}
	catch (Exception $e) {
		echo $e->getBody();
	}
}

// Access token ---------------------------------------------------------------
if ($action === 'accessToken') {
	$consumer->setToken($_SESSION[$storeKey_oatuhRequestToken]);
	$consumer->setTokenSecret($_SESSION[$storeKey_oatuhRequestTokenSecret]);
	$consumer->accept($request);
	try {
		$consumer->getAccessToken($access_token_url, $params['oauth_verifier']);
		$_SESSION[$storeKey_oatuhAccessToken] = $consumer->getToken();
		$_SESSION[$storeKey_oatuhAccessTokenSecret] = $consumer->getTokenSecret();
		unset($_SESSION[$storeKey_oatuhRequestToken]);
		unset($_SESSION[$storeKey_oatuhRequestTokenSecret]);
		echo 'Authorization succeeded!';
	}
	catch (HTTP_OAuth_Consumer_Exception_InvalidResponse $e) {
		echo $e->getBody();
	}
	catch (Exception $e) {
		echo $e->getMessage();
	}
}

// Logout ---------------------------------------------------------------
if ($action === 'logout') {
	unsetSessionValue($storeKey_oatuhRequestToken);
	unsetSessionValue($storeKey_oatuhRequestTokenSecret);
	unsetSessionValue($storeKey_oatuhAccessToken);
	unsetSessionValue($storeKey_oatuhAccessTokenSecret);
}

// Other code -----------------------------------------------------------------

function unsetSessionValue($key) {
	if (array_key_exists($key, $_SESSION)) {
		unset($_SESSION[$key]);
	}
}

include './include_authPanel.php';

?>